package com.itboye.pondteam.pondteam;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.pondteam.R;
import com.itboye.pondteam.app.MyApplication;
import com.itboye.pondteam.base.BaseActivity;
import com.itboye.pondteam.bean.DeviceListBean;
import com.itboye.pondteam.db.DBManager;
import com.itboye.pondteam.presenter.UserPresenter;
import com.itboye.pondteam.utils.Const;
import com.itboye.pondteam.utils.loadingutil.MAlert;
import com.itboye.pondteam.volley.ResultEntity;

import java.util.Observable;
import java.util.Observer;

import static com.itboye.pondteam.utils.EmptyUtil.getSp;

@SuppressLint("NewApi")
public class ManualAddDeviceActivity extends BaseActivity implements Observer {

    MyApplication mApp;
    EditText manual_add_device_edit_did;
    EditText mEditTextPassword;
    EditText manual_add_device_edit_nickname;
    Button mButtonDelete;
    Context mContext;
    ManualAddDeviceActivity mThis;
    UserPresenter userPresenter;
    ImageView img_right;
    TextView txt_title;
    EditText manual_add_device_edit_password;
    DBManager dbManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_manual_add_device);
        mApp = (MyApplication) getApplication();
        mApp.mManualAddDeviceUi = this;
        mContext = this;
        mThis = this;
        dbManager=new DBManager(this);
        userPresenter=new UserPresenter(this);
        txt_title.setText(getString(R.string.add_device));
        img_right.setBackgroundResource(R.drawable.ic_action_accept);
//		mDeviceInfo = new AqDeviceInfo();
        mEditTextPassword = (EditText) findViewById(R.id.manual_add_device_edit_nickname);
//		mEditTextName.setText(mDeviceInfo.mDeviceName);
//		Intent intent=new Intent("SendFinish");
//		sendBroadcast(intent);
    }



    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // 如果是返回键
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            mApp.mManualAddDeviceUi = null;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.img_right:
                String did = manual_add_device_edit_did.getText().toString();
                String nickName = manual_add_device_edit_nickname.getText().toString();
                if (did.equals("")) {
                    MAlert.alert(getString(R.string.deviceid_empty));
                    return;
                }
                if (nickName.equals("")) {
                    MAlert.alert(getString(R.string.device_name_empty));
                    return;
                }
                boolean hasAdd = false;
                //排除已经添加过的设备
                if (mApp.deviceListActivityUI.arrayList != null) {
                    for (DeviceListBean deviceListBean : mApp.deviceListActivityUI.arrayList) {
                        if (deviceListBean.getDid().contains(did)) {
                            hasAdd = true;
                            break;
                        }
                    }
                }
                if (hasAdd) {
                    MAlert.alert(getString(R.string.hasAdd));
                    return;
                }
                String psw = manual_add_device_edit_password.getText().toString();
                if (psw.equals("")) {
                    MAlert.alert(getString(R.string.psw_name_empty));
                    return;
                }
                int count = dbManager.queryDeviceCountByDid(did,getSp(Const.UID));
                if (count > 0) {
                    //先删除数据库设备，再进行添加
                    dbManager.deleteDeviceDataByDid(did,getSp(Const.UID));
                }
                dbManager.insertDeviceData(did, psw,getSp(Const.UID));
                userPresenter.addDevice(getSp(Const.UID),did,nickName);
                break;
        }
    }

    @Override
    public void update(Observable o, Object data) {
        ResultEntity entity=handlerError(data);
        if (entity!=null) {
            if (entity.getCode()!=0) {
                MAlert.alert(entity.getMsg());
                return;
            }
            if (entity.getEventType()== UserPresenter.adddevice_success) {
                MAlert.alert(entity.getData());
                mApp.deviceListActivityUI.getMyDeviceList();
                finish();
                //结束上一个activity
//                mApp.addDeviceUI.finish();
                //结束当前activity
            }else  if (entity.getEventType()== UserPresenter.adddevice_fail) {
                MAlert.alert(entity.getData());
            }
        }
    }
}
